#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__autho__ = "Tony.Don"
__lastupdatetime__ = "2017/09/20"

from html.parser import HTMLParser
from html.entities import name2codepoint

'''
class\r\n
    core.parser.parserHtml(HTMLParser)\r\n
description\r\n
    分析html\r\n
'''
class parserHtml(HTMLParser):
    method = None
    def __init__(self,func):
        super(parserHtml,self).__init__()
        self.method=func

    def handle_starttag(self,tag,attrs):
        self.method.s_tag(tag,attrs)

    def handle_endtag(self, tag):
        self.method.e_tag(tag)
    
    def handle_data(self, data):
        self.method.data(data)

    def handle_comment(self, data):
        self.method.comment(data)

    def handle_entityref(self, name):
        c = chr(name2codepoint[name])
        self.method.entityref(c)

    def handle_charref(self, name):
        if name.startswith('x'):
            c = chr(int(name[1:], 16))
        else:
            c = chr(int(name))
        self.method.charref(c)

    def handle_decl(self, data):
        self.method.decl(data)